﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Xml;
using System.Collections.Specialized;

namespace ETOrePriceCalc
{
	public class ApiParser
	{
		private Dictionary<string, int> _idList;
		private Dictionary<int, double> _priceList;

		private string _charname;

		static string _priceUrl = "http://api.eve-marketdata.com/api/item_prices2.txt?";
		int _regionId;
		int _solarsystemId;
		
		/// <summary>
		/// creates a new api parser
		/// </summary>
		/// <param name="charname">charname that will be use for api calls</param>
		/// <param name="regionId">id of the region to get the price for, default to jita</param>
		/// <param name="solarsystemId">id of the solarsystem to check the price for</param>
		public ApiParser(string charname, int regionId, int solarsystemId)
		{
			_charname = charname;
			_regionId = regionId;
			_priceList = new Dictionary<int, double>();
			_idList = new Dictionary<string, int>();

			//add all the items we want to lookup
			_idList.Add("Veldspar",1230);
			_idList.Add("Concentrated Veldspar",17470);
			_idList.Add("Dense Veldspar",17471);
			_idList.Add("Scordite",1228);
			_idList.Add("Condensed Scordite",17463);
			_idList.Add("Massive Scordite",17464);
			_idList.Add("Pyroxeres",1224);
			_idList.Add("Solid Pyroxeres",17459);
			_idList.Add("Viscous Pyroxeres",17460);
			_idList.Add("Plagioclase",18);
			_idList.Add("Azure Plagioclase",17455);
			_idList.Add("Rich Plagioclase",17456);
			_idList.Add("Omber",1227);
			_idList.Add("Silvery Omber",17867);
			_idList.Add("Golden Omber",17868);
			_idList.Add("Kernite",20);
			_idList.Add("Luminous Kernite",17452);
			_idList.Add("Fiery Kernite",17453);
			_idList.Add("Jaspet",1226);
			_idList.Add("Pure Jaspet",17448); 
			_idList.Add("Pristine Jaspet",17449); 
			_idList.Add("Hemorphite",1231);
			_idList.Add("Vivid Hemorphite",17444); 
			_idList.Add("Radiant Hemorphite",17445); 
			_idList.Add("Hedbergite",21);
			_idList.Add("Vitric Hedbergite",17440); 
			_idList.Add("Glazed Hedbergite",17441); 
			_idList.Add("Gneiss",1229);
			_idList.Add("Iridescent Gneiss",17865); 
			_idList.Add("Prismatic Gneiss",17866);
			_idList.Add("Dark Ochre",1232);
			_idList.Add("Onyx Ochre",17436); 
			_idList.Add("Obsidian Ochre",17437); 
			_idList.Add("Crokite",1225);
			_idList.Add("Sharp Crokite",17432); 
			_idList.Add("Crystalline Crokite",17433); 
			_idList.Add("Spodumain",19);
			_idList.Add("Bright Spodumain",17466); 
			_idList.Add("Gleaming Spodumain",17467); 
			_idList.Add("Bistot",1223);
			_idList.Add("Triclinic Bistot",17428); 
			_idList.Add("Monoclinic Bistot",17429); 
			_idList.Add("Arkonor",22);
			_idList.Add("Crimson Arkonor",17425); 
			_idList.Add("Prime Arkonor",17426); 
			_idList.Add("Mercoxit",11396);
			_idList.Add("Magma Mercoxit",17869);
			_idList.Add("Vitreous Mercoxit", 17870);
		}

        /// <summary>
        /// region id used for the price lookup
        /// </summary>
        public int Region
        {
            get { return _regionId; }
            set { _regionId = value; }
        }

        /// <summary>
        /// solarsystem id used for the price lookup (not currently used)
        /// </summary>
        public int SolarSystem
        {
            get { return _solarsystemId; }
            set { _solarsystemId = value; }
        }

		/// <summary>
		/// gets all the mineral prices from the server
		/// </summary>
		/// <returns>return True if successfull otherwise false</returns>
		public void GetPrices() 
		{
			//construct the string to make the request

			//get a list of all required id's
			string itemIds = "";

			foreach (int i in _idList.Values)
			{
				itemIds += i + ",";
			}

			string sendstring = "";
            sendstring = _priceUrl + "char_name=" + _charname + "&type_ids=" + itemIds + "&solarsystem_ids=" + _solarsystemId + "&buysell=s";

			//get the string from the site
			string answer = new System.Net.WebClient().DownloadString(sendstring);

			//read reply
			//s         34      10000002	4.8336881426722     2013-08-30  17:14:49
			//buy/sell  itemid  regionId    price               date        time


			//parse reply to usable data (write to _priceList)
			string[] lines = answer.Split(new string[] {"\n"}, StringSplitOptions.RemoveEmptyEntries);
			
			string[] split;
			foreach (string s in lines)
			{
				split = s.Split('\t');
				int id = Convert.ToInt32(split[1]);
				_priceList[id] = XmlConvert.ToDouble(split[3]);
			}

		}

		public void SetPrices (List<Mineral> list)
		{
			foreach (Mineral m in list)
			{
                if(_idList.ContainsKey(m.Name))
				    m.Price = _priceList[_idList[m.Name]];
			}
		}
	}
}
